﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms.Integration;
using System.IO.Ports;

namespace OpticalPowerMeter.UserControls
{
    /// <summary>
    /// 输入串口名，设置波特率的控件
    /// </summary>
    public partial class SerialPortControl : UserControl
    {
        public SerialPortControl()
        {
            InitializeComponent();
            InitBaudrateArr();
        }


        //设置波特率可选项
        private void InitBaudrateArr()
        {
            string[] baudrateArr = { "9600","19200","115200" };
            BaudrateBox.ItemsSource = baudrateArr;
            BaudrateBox.SelectedIndex = 0;
        }


        //文本框得到焦点时，提供自动补全信息。按照可用串口名补充
        private void TextBox_GetFocus(object obj, RoutedEventArgs args)
        {
            string[] portnameArrange = SerialPort.GetPortNames();
            WindowsFormsHost host = obj as WindowsFormsHost;
            System.Windows.Forms.Control control = host.Child;
            System.Windows.Forms.TextBox textBox = (System.Windows.Forms.TextBox)control;
            System.Windows.Forms.AutoCompleteStringCollection auto = new System.Windows.Forms.AutoCompleteStringCollection();
            auto.AddRange(portnameArrange);
            textBox.AutoCompleteCustomSource = auto;
            textBox.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
            textBox.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.CustomSource;
            textBox.Text = textBox.Text.ToUpper();
        }

        #region 控件尺寸属性
        //字号
        static DependencyProperty MyFontSizeProperty = DependencyProperty.Register(
            "MyFontSize",
            typeof(double),
            typeof(SerialPortControl)
            );

        //串口名文本框边距
        static DependencyProperty PortnameBoxMarginProperty = DependencyProperty.Register(
            "PortnameBoxMargin",
            typeof(Thickness),
            typeof(SerialPortControl)
            );

        //输入框高度
        static DependencyProperty BoxHeightProperty = DependencyProperty.Register(
            "BoxHeight",
            typeof(double),
            typeof(SerialPortControl)
            );

        //输入框宽度
        static DependencyProperty BoxWidthProperty = DependencyProperty.Register(
            "BoxWidth",
            typeof(double),
            typeof(SerialPortControl)
            );

        //波特率Label边距
        static DependencyProperty BaudrateLabelMarginProperty = DependencyProperty.Register(
            "BaudrateLabelMargin",
            typeof(Thickness),
            typeof(SerialPortControl)
            );

        //波特率选择框的边距
        static DependencyProperty BaudrateBoxMarginProperty = DependencyProperty.Register(
            "BaudrateBoxMargin",
            typeof(Thickness),
            typeof(SerialPortControl)
            );

        static DependencyProperty BaudrateBoxWidthProperty = DependencyProperty.Register(
            "BaudrateBoxWidth",
            typeof(double),
            typeof(SerialPortControl)
            );
        #endregion


        #region 串口名、波特率
        public string Portname
        {
            get
            {
                string portname = "";
                App.Current.Dispatcher.Invoke(new Action(()=> {
                    portname = PortnameBox.Child.Text.Trim();
                }));
                return portname;
            }
            set
            {
                App.Current.Dispatcher.Invoke(new Action(()=>{
                    PortnameBox.Child.Text = value;
                }));
            }
        }

        public string Baudrate
        {
            get
            {
                string baudrate = "";
                App.Current.Dispatcher.Invoke(new Action(()=> {
                    baudrate = BaudrateBox.SelectedValue.ToString();
                }));
                return baudrate;
            }
            set
            {
                App.Current.Dispatcher.Invoke(new Action(()=> {
                    BaudrateBox.SelectedValue = value; 
                }));
            }
        }
        #endregion


        //控件尺寸发生变化时，调整字号、位置等内容
        private void Size_Changed(object sender,SizeChangedEventArgs args) {
            double thisWidth = ActualWidth;
            double thisHeight = ActualHeight;
            #region 设置字号
            if (thisWidth / thisHeight > 7) //如果长高比大于7，则按照高度计算字号，否则按照宽度计算字号
            {
                double fontSize = Math.Round((thisHeight - 10) / 1.27,1);
                SetValue(MyFontSizeProperty,fontSize);
            }
            else
            {
                double fontSize = Math.Round((thisWidth-10)/14.16,1);
                SetValue(MyFontSizeProperty,fontSize);
            }
            #endregion
            #region 根据字号计算串口名输入框的左边距
            double myFontSize = (double)GetValue(MyFontSizeProperty);
            double portnameTextLeft = (myFontSize * 4 + 15);
            SetValue(PortnameBoxMarginProperty, new Thickness(portnameTextLeft,0,0,0));
            #endregion
            #region 根据字号计算输入框高度
            SetValue(BoxHeightProperty, myFontSize * 1.27);
            #endregion
            #region 根据字号计算串口名输入框宽度
            double boxWidth = myFontSize * 3.64 + 10;
            SetValue(BoxWidthProperty, boxWidth);
            SetValue(BaudrateBoxWidthProperty, myFontSize * 4.64);
            #endregion
            #region 计算波特率Label的左边距
            double baudrateLabelLeft = portnameTextLeft + boxWidth + 5;
            SetValue(BaudrateLabelMarginProperty,new Thickness(baudrateLabelLeft,0,0,0));
            #endregion
            #region 计算波特率文本框左边距
            double baudrateBoxLeft = baudrateLabelLeft + myFontSize * 3 + 15;
            SetValue(BaudrateBoxMarginProperty,new Thickness(baudrateBoxLeft,0,0,0));
            #endregion
        }
    }
}
